package com.cloudlead.fleetAction.service.impl;

import com.cloudlead.common.lang.persistence.*;
import com.cloudlead.commonData.service.*;
import com.cloudlead.commonData.vo.*;
import com.cloudlead.fleetAction.service.FleetOrderSer;
import com.cloudlead.fleetService.vo.FleetOrderVo;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by 20150817 on 2015/9/1.
 */
@Service
public class FleetOrderSerImpl implements FleetOrderSer{
    @Autowired
    TbCustomerOrderService customerOrderService;
    @Autowired
    BaseCompanyUserService baseCompanyUserService;
    @Autowired
    BaseCompanyService baseCompanyService;
    @Autowired
    TbFleetOrderService fleetOrderService;
    @Autowired
    TbContainerInfoService containerInfoService;
    @Autowired
    BaseVoyageService baseVoyageService;
    @Autowired
    TbOrderstatusService orderstatusService;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    @Override
    public Page<FleetOrderVo> findAllFleetOrder(FleetOrderVo vo, int page, int size) {
        FleetOrderVo fto = new FleetOrderVo();
        if(vo!=null){//如果vo不为空
            fto=vo;
        }
        PageRequest pr = new PageRequest();
        List<Term> tr = new ArrayList<Term>();
        tr.add(new Term("flag","0"));//未删除

        if(fto.getOrderType()!=null&&!fto.getOrderType().equals("")){//类型
            tr.add(new Term("orderType",fto.getOrderType()));
        }
        if(fto.getCustomerCode()!=null&&!fto.getCustomerCode().equals("")){//类型
            tr.add(new Term("customerCode",fto.getCustomerCode()));
        }
        if(fto.getOrderState()!=null&&(!fto.getOrderState().equals(""))) {
            tr.add(new Term("orderState",fto.getOrderState()));
        }
        if(fto.getPolName()!=null&&!fto.getPolName().equals("")){
            tr.add(new Term("polName",Operator.LK,"%"+fto.getPolName()+"%"));
        }
        if(fto.getDelName()!=null&&!fto.getDelName().equals("")){
            tr.add(new Term("delName",Operator.LK,"%"+fto.getDelName()+"%"));
        }
        List<Term> ctnTrList=new ArrayList<Term>();

        if(fto.getContainerNo()!=null&&(!fto.getContainerNo().equals(""))) {
            ctnTrList.add(new Term("containerNo", Operator.LK, "%"+fto.getContainerNo() + "%"));
        }
        if (ctnTrList.size()>0){
            Filter filter =new Filter(new Criteria(ctnTrList));
            List<TbContainerInfo> flList=containerInfoService.list(filter,null);
            List coIdList= new ArrayList<>();
            for(TbContainerInfo co:flList) {
                coIdList.add(co.getFleetOrder().getId());
            }
            tr.add(new Term("id", Operator.IN, coIdList));
        }


        if (fto.getIsStart()!=null &&!fto.getIsStart().equals("")) {
            tr.add(new Term("isStart", fto.getIsStart()));
        }
        if (fto.getBussinessCode()!=null &&!fto.getBussinessCode().equals("")){
            tr.add(new Term("bussinessCode",Operator.LK,"%"+fto.getBussinessCode()+"%"));
        }
      //  if(fto.getFleetId() != null&& !fto.getFleetId().equals("")){
            tr.add(new Term("fleetCompany.id",fto.getFleetId()));
      //  }
        //按装箱地址模糊查询车队订单
        if(fto.getPackagingAddress() != null&& !fto.getPackagingAddress().equals("")){
            tr.add(new Term("packagingAddress",Operator.LK,"%"+fto.getPackagingAddress()+"%"));
        }
        //按拆箱地址模糊查询车队订单
        if(fto.getRemoveAddress() != null&& !fto.getRemoveAddress().equals("")){
            tr.add(new Term("removeAddress",Operator.LK,"%"+fto.getRemoveAddress()+"%"));
        }
        //按船名模糊查询车队订单
        List<Term> trVoyage = new ArrayList<Term>();
        if(fto.getVessel() != null&& !fto.getVessel().equals("")) {
            trVoyage.add(new Term("vessel", Operator.LK, "%" + fto.getVessel() + "%"));
        }
        if(fto.getVoyage() != null&& !fto.getVoyage().equals("")) {
            trVoyage.add(new Term("voyage", Operator.LK, "%" + fto.getVoyage() + "%"));
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if(fto.getArriveExpectedTime()!=null&&!fto.getArriveExpectedTime().equals("")) {
            try {
                trVoyage.add(new Term("arriveExpectedTime",sdf.parse(fto.getArriveExpectedTime())));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        if(fto.getStartExpectedTime()!=null&&!fto.getStartExpectedTime().equals("")){
            try {
                trVoyage.add(new Term("startExpectedTime",sdf.parse(fto.getStartExpectedTime())));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        if (trVoyage.size()>0){
            Filter filter =new Filter(new Criteria(trVoyage));
            List<BaseVoyage> bv=baseVoyageService.list(filter,null);
            List boIdList= new ArrayList<>();
            for(BaseVoyage bo:bv) {
                boIdList.add(bo.getId());
            }
            tr.add(new Term("baseVoya.id", Operator.IN, boIdList));
        }

        Filter filter = new Filter(new Criteria(tr));
        pr.setPage(page);
        pr.setSize(size);
        pr.setFilter(filter);
        Page<TbFleetOrder> pb = (Page<TbFleetOrder>)fleetOrderService.findAll(pr);
        List<TbFleetOrder> lx1 = new ArrayList<TbFleetOrder>();

        for(Iterator<TbFleetOrder> it = pb.iterator();it.hasNext();) {//遍历
            TbFleetOrder tfi = it.next();
            if(tfi.getTbCustomerOrder()!=null&&tfi.getTbCustomerOrder().getId()!=null){//订单ID不为空
                TbCustomerOrder tco= customerOrderService.view(tfi.getTbCustomerOrder().getId());//查出订单信息
                tfi.setTbCustomerOrder(tco);
            }
            lx1.add(tfi);
        }
        pb.setContent(lx1);

        Page<FleetOrderVo> pbv = new Page<FleetOrderVo>();
        BeanUtils.copyProperties(pb,pbv);
        return pbv;
    }

    @Override
    public String createOrder(FleetOrderVo vo) {
        TbFleetOrder tfo = new TbFleetOrder();
        BeanUtils.copyProperties(vo, tfo);
        tfo.setFlag("0");
        tfo.setOrderType("0");
        if(vo.getFleetId()!=null&&!vo.getFleetId().equals("")){
            BaseCompany baseCompany = new BaseCompany();
            baseCompany.setId(vo.getFleetId());
            tfo.setFleetCompany(baseCompany);
        }
        if(vo.getCustomerCode()!=null&&!vo.getCustomerCode().equals("")){
            BaseFleetCustomer baseFleetCustomer = new BaseFleetCustomer();
            baseFleetCustomer.setId(vo.getCustomerCode());
            tfo.setCustomerCo(baseFleetCustomer);
        }
        if(vo.getOrderState()!=null&&!vo.getOrderState().equals("")){
            tfo.setOrderState(vo.getOrderState());
        }
        if(vo.getCreateTime()!=null&&!vo.getCreateTime().equals("")) {//订单时间
            tfo.setCreateTime(getDate(vo.getCreateTime()));
        }else{
            tfo.setCreateTime(null);
        }
        fleetOrderService.create(tfo);
        return tfo.getId();
    }

    @Override
    public void updateOrder(FleetOrderVo vo) {
        TbFleetOrder tfo = fleetOrderService.view(vo.getId());
        BeanUtils.copyProperties(vo, tfo);
        tfo.setFlag("0");
        tfo.setOrderType("0");
        if(vo.getFleetId()!=null&&!vo.getFleetId().equals("")){
            BaseCompany baseCompany = new BaseCompany();
            baseCompany.setId(vo.getFleetId());
            tfo.setFleetCompany(baseCompany);
        }
        if(vo.getCustomerCode() != null && !vo.getCustomerCode().equals("")){
            BaseFleetCustomer baseFleetCustomer = new BaseFleetCustomer();
            baseFleetCustomer.setId(vo.getCustomerCode());
            tfo.setCustomerCo(baseFleetCustomer);
        }
        if(vo.getOrderState()!=null&&!vo.getOrderState().equals("")){
            tfo.setOrderState(vo.getOrderState());
        }
        if(vo.getCreateTime()!=null&&!vo.getCreateTime().equals("")) {//订单时间
            tfo.setCreateTime(getDate(vo.getCreateTime()));
        }else{
            tfo.setCreateTime(null);
        }
        fleetOrderService.update(tfo);
    }

    @Override
    public void deleteOrder(String id) {
        TbFleetOrder tfo = fleetOrderService.view(id);
        tfo.setFlag("1");
        fleetOrderService.update(tfo);
    }

    //时间格式转换
    private DateTime getDate(String sDate){
        Date d= null;
        try {
            d = sdf.parse(sDate);//string 转换 date
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new DateTime(d);//date 转换 dateTime 保存
    }
    //订单审核通过
    public void auditOk(FleetOrderVo vo){
        TbFleetOrder fl=fleetOrderService.view(vo.getId());
        fl.setOrderState("未执行");
        TbOrderstatus os=new TbOrderstatus();
        os.setContent("订单审核通过");
        TbCustomerOrder cs=fl.getTbCustomerOrder();
        os.setCustomerOrder(cs);
        os.setFleetOrder(fl);
        DateTime date=new DateTime();
        os.setTime(date);
        BaseCompany fleet=fl.getFleetCompany();
        String fleetId=fleet.getId();
        List<Term> tr = new ArrayList<Term>();
        tr.add(new Term("baseCompanyId", fleetId));
        Filter filter = new Filter(new Criteria(tr));
        BaseCompanyUser bs=baseCompanyUserService.findOne(filter);
        String op=bs.getUserName();
        os.setOperator(op);
        orderstatusService.create(os);
        fleetOrderService.update(fl);
    }
    //拒绝订单
    public void auditNo(FleetOrderVo vo){
        TbFleetOrder fl=fleetOrderService.view(vo.getId());
        fl.setOrderState("已拒绝");
        TbOrderstatus os=new TbOrderstatus();
        String content=vo.getContent();
        os.setContent(content);
        TbCustomerOrder cs=fl.getTbCustomerOrder();
        os.setCustomerOrder(cs);
        os.setFleetOrder(fl);
        DateTime date=new DateTime();
        os.setTime(date);
        BaseCompany fleet=fl.getFleetCompany();
        String fleetId=fleet.getId();
        List<Term> tr = new ArrayList<Term>();
        tr.add(new Term("baseCompanyId", fleetId));
        Filter filter = new Filter(new Criteria(tr));
        BaseCompanyUser bs=baseCompanyUserService.findOne(filter);
        String op=bs.getUserName();
        os.setOperator(op);
        orderstatusService.create(os);
        fleetOrderService.update(fl);
    }
}

